﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using NHibernate.Cfg;
using System.IO;
using System.Reflection;
using NHibernate.Mapping.ByCode;
using Domain.Model.Map;
using NHibernate.Mapping.ByCode.Conformist;

namespace Dao.Repository
{
    public class MyLocalSessionFactoryObject : LocalSessionFactoryObject
    {
        /// <summary>
        /// 实体类所在的程序集的名称
        /// </summary>
        public string[] ModelAssemblyName { get; set; }

        /// <summary>
        /// Nhibernate ByCode 自带映射方式  此种方式映射源代码
        /// </summary>
        /// <param name="config"></param>
        protected override void PostProcessConfiguration(Configuration config)
        {
            base.PostProcessConfiguration(config);
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();//Profiler监控记录日志
            ModelMapper mappers = new ModelMapper();
            foreach (string modelAssemblyName in ModelAssemblyName)
            {
                Assembly assembly = Assembly.Load(modelAssemblyName);
                mappers.AddMappings(assembly.GetExportedTypes());
                var hbmMapping = mappers.CompileMappingForAllExplicitlyAddedEntities();
                // Console.WriteLine(hbmMapping.AsString());
                config.AddDeserializedMapping(hbmMapping, "");
            }
        }

    }
}
